﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using KPIS.GERP.Models.ICM;


namespace KPIS.GERP.WEB.GIMS.Controllers
{
    public class InstallmentItemController : Controller
    {
        private PurchaseOrderService.IPurchaseOrderService purchaseOrderService = new PurchaseOrderService.PurchaseOrderServiceClient(); 
        private PurchaseRequestService.PurchaseRequestServiceClient purchaseRequestServiceClient = new PurchaseRequestService.PurchaseRequestServiceClient();
        private PurchaseRequestItemService.PurchaseRequestItemServiceClient purchaseRequestItemServiceClient = new PurchaseRequestItemService.PurchaseRequestItemServiceClient();
        private BidService.BidServiceClient bidServiceClient = new BidService.BidServiceClient();

        public ActionResult Index(int poInstallId)
        {
            ViewBag.PoInstallId = poInstallId;
            ViewBag.PoId = PurchaseOrder(poInstallId).PoId;
           return View(PurchaseOrderInstallmentItem(poInstallId));
        }
        
        private List<PurchaseRequestItem> PurchaseRequestItem (int poInstallId)
        {
            return purchaseRequestItemServiceClient.Select(new PurchaseRequestItem
                {
                    PrId = PurchaseRequest(poInstallId).PrId
                }, null).ToList();
        }
        
        private PurchaseRequest PurchaseRequest (int poInstallId)
        { 
            return purchaseRequestServiceClient.Select(new PurchaseRequest
                {
                    PrId = Bidder(poInstallId).PrId.Value
                }, null).FirstOrDefault();
        }
        
        private Bidder Bidder (int poInstallId)
        {
            var purchaseOrder = PurchaseOrder(poInstallId);
            var bidder = bidServiceClient.SelectBidder(new Bidder
                {
                    BidderId = purchaseOrder.BidderId.Value
                }, null).FirstOrDefault();
            return bidder;

        }
        private PurchaseOrder PurchaseOrder (int poInstallId)
        {
            return purchaseOrderService.Select(new PurchaseOrder
                {
                    PoId = PurchaseOrderInstallment(poInstallId).PoId.Value
                }, null).FirstOrDefault();
        }

        private PurchaseOrderInstallment PurchaseOrderInstallment (int poInstallId)
        {
            return purchaseOrderService.SelectPoInstallment(new PurchaseOrderInstallment
                {
                    PoInstallId = poInstallId
                }, null).FirstOrDefault();
        }

        private List<PurchaseOrderInstallmentItem> PurchaseOrderInstallmentItem (int poInstallId)
        {
            return purchaseOrderService.SelectPoInstallmentItem(new PurchaseOrderInstallmentItem
                {
                    PoInstallId = poInstallId
                }, null).ToList();
        }
    }
}
